package kr.hkit.androidcomplete03.ETC;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class ResultActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.voteresult);
		
		Intent intent = getIntent();
		int[] voteResult = intent.getIntArrayExtra("VoteCount");
		String[] imageName = intent.getStringArrayExtra("ImageName");
		
		TextView[] tv = new TextView[imageName.length];
		RatingBar[] rbar = new RatingBar[imageName.length];
		
		TextView winTv = (TextView) findViewById(R.id.wintitle);
		ImageView winImg = (ImageView) findViewById(R.id.winimg);
		
		Integer[] tvID = {R.id.votetv1, R.id.votetv2, R.id.votetv3, R.id.votetv4, R.id.votetv5, R.id.votetv6, R.id.votetv7, R.id.votetv8, R.id.votetv9};
		Integer[] rbarID = {R.id.ratingBar1, R.id.ratingBar2, R.id.ratingBar3, R.id.ratingBar4, R.id.ratingBar5, R.id.ratingBar6, R.id.ratingBar7, R.id.ratingBar8, R.id.ratingBar9 };
		Integer[] imageFieldID = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9 };
		
		for(int i = 0; i < voteResult.length; i++){
			tv[i] = (TextView) findViewById(tvID[i]);
			rbar[i] = (RatingBar) findViewById(rbarID[i]);
		}
		
		for(int i = 0; i < voteResult.length; i++){
			tv[i].setText(imageName[i]);
			rbar[i].setRating(voteResult[i]);
		}
		
		int maxEntry = 0;
		for(int i = 1; i < voteResult.length; i++){
			if(voteResult[maxEntry] < voteResult[i]){
				maxEntry = i;
			}
		}
		winTv.setText(imageName[maxEntry]);
		winImg.setImageResource(imageFieldID[maxEntry]);
		
		Button btnReturn = (Button) findViewById(R.id.votebackbtn);
		btnReturn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
			
		});
	}
}
